import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_test/message.dart';

import 'flutter_plugin_test_platform_interface.dart';

/// 方法通道实现颤振插件测试
class MethodChannelFlutterPluginTest extends FlutterPluginTestPlatform {
  /// 方法通道
  @visibleForTesting
  final methodChannel = const MethodChannel('methodChannel');

  /// 消息通道
  @visibleForTesting
  final BasicMessageChannel<Object?> basicMessageChannel = const BasicMessageChannel<Object?>('messageChanel', StandardMessageCodec(),);

  ///获取当前位置
  @override
  Future<PositionInfo> getCurrentPosition() async {
    MusicPosition? musicPosition;
    final Map<Object?, Object?>? replyMap = await basicMessageChannel.send(null) as Map<Object?, Object?>?;
    if (replyMap == null) {
      throw PlatformException(code: 'channel-error', message: '无法建立连接通道。', details: null,);
    } else if (replyMap['error'] != null) {
      final Map<Object?, Object?> error = (replyMap['error'] as Map<Object?, Object?>?)!;
      throw PlatformException(code: (error['code'] as String?)!, message: error['message'] as String?, details: error['details'],);
    } else {
      musicPosition = MusicPosition.decode(replyMap['result']!);
    }
    return PositionInfo(position: musicPosition.position!, duration: musicPosition.duration!);
  }

  @override
  ///得到平台的版本
  Future<String?> getPlatformVersion() async {
    ///方法通道.调用方法<String>("得到平台的版本")
    final result = await methodChannel.invokeMethod<String>('getPlatformVersion');
    ///返回版本
    return result;
  }

  @override
  ///initMusics
  Future<List<Map<String, dynamic>>> initMusics() async {
    final result = await methodChannel.invokeMethod<List<Object?>>('initMusics');
    List<dynamic> list = result as List<dynamic>;
    List<Map<String, dynamic>> list2 = list.map((element) {
      Map<String, dynamic> pigeonMap = {};
      final Map<Object?, Object?> e = element as Map<Object?, Object?>;
      pigeonMap["musicFileUrl"] = e["musicFileUrl"];
      pigeonMap["musicTitle"] = e["musicTitle"];
      pigeonMap["musicUrl"] = e["musicUrl"];
      pigeonMap["musicId"] = e["musicId"];
      pigeonMap["musicDuration"] = e["musicDuration"];
      pigeonMap["musicFileName"] = e["musicFileName"];
      pigeonMap["musicAuthor"] = e["musicAuthor"];
      return pigeonMap;
    }).toList();
    return list2;
  }

  @override
  ///playMusic
  Future<String?> playMusic(int index) async {
    print("index ${index}");
    final result = await methodChannel.invokeMethod<String>('playMusic',{"index":index});
    return result;
  }

  @override
  ///startMusic
  Future<String?> startMusic() async {
    final result = await methodChannel.invokeMethod<String>('startMusic');
    return result;
  }

  @override
  ///stopMusic
  Future<String?> stopMusic() async {
    final result = await methodChannel.invokeMethod<String>('stopMusic');
    return result;
  }

  @override
  ///lastClick
  Future<String?> lastClick() async {
    final result = await methodChannel.invokeMethod<String>('lastClick');
    return result;
  }

  @override
  ///nextClick
  Future<String?> nextClick() async {
    final result = await methodChannel.invokeMethod<String>('nextClick');
    return result;
  }
}

class PositionInfo{
  final int position;
  final int? duration;

  PositionInfo({required this.position, this.duration});
}
